package com.ruoyi.zcxt.service.impl;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zcxt.constant.ZcxtConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zcxt.mapper.ZcLocationMapper;
import com.ruoyi.zcxt.domain.ZcLocation;
import com.ruoyi.zcxt.service.IZcLocationService;

/**
 * 资产位置（树）Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@Service
public class ZcLocationServiceImpl implements IZcLocationService 
{
    @Autowired
    private ZcLocationMapper zcLocationMapper;

    /**
     * 查询资产位置（树）
     * 
     * @param lcId 资产位置（树）主键
     * @return 资产位置（树）
     */
    @Override
    public ZcLocation selectZcLocationByLcId(Long lcId)
    {
        return zcLocationMapper.selectZcLocationByLcId(lcId);
    }

    /**
     * 查询资产位置（树）列表
     * 
     * @param zcLocation 资产位置（树）
     * @return 资产位置（树）
     */
    @Override
    public List<ZcLocation> selectZcLocationList(ZcLocation zcLocation)
    {
        return zcLocationMapper.selectZcLocationList(zcLocation);
    }

    /**
     * 新增资产位置（树）
     * 
     * @param zcLocation 资产位置（树）
     * @return 结果
     */
    @Override
    public int insertZcLocation(ZcLocation zcLocation)
    {
        ZcLocation info = zcLocationMapper.selectZcLocationByLcId(zcLocation.getLcId());
        if (info == null && zcLocation.getParentId() == 0) {
            zcLocation.setAncestors("0");
        }else {
            if (!ZcxtConstants.Location_NORMAL.equals(info.getStatus())) {
                throw new ServiceException("位置分类停用，不允许新增");
            }
            zcLocation.setAncestors(info.getAncestors() + "," + zcLocation.getParentId());
        }
        zcLocation.setCreateTime(DateUtils.getNowDate());
        return zcLocationMapper.insertZcLocation(zcLocation);
    }

    /**
     * 修改资产位置（树）
     * 
     * @param zcLocation 资产位置（树）
     * @return 结果
     */
    @Override
    public int updateZcLocation(ZcLocation zcLocation)
    {
        ZcLocation newParentLocation = zcLocationMapper.selectZcLocationByLcId(zcLocation.getParentId());
        ZcLocation oldLocation = zcLocationMapper.selectZcLocationByLcId(zcLocation.getLcId());
        if (StringUtils.isNotNull(newParentLocation) && StringUtils.isNotNull(oldLocation)) {
            String newAncestors = newParentLocation.getAncestors() + "," + newParentLocation.getDeptId();
            String oldAncestors = oldLocation.getAncestors();
            zcLocation.setAncestors(newAncestors);
            updateLocationChildren(zcLocation.getLcId(), newAncestors, oldAncestors);
        }
        zcLocation.setUpdateTime(DateUtils.getNowDate());
        int result = zcLocationMapper.updateZcLocation(zcLocation);
        if (ZcxtConstants.Location_NORMAL.equals(zcLocation.getStatus()) && StringUtils.isNotEmpty(zcLocation.getAncestors())
                && !StringUtils.equals("0",zcLocation.getAncestors())) {
            // 如果该分类是启用状态，则启用该分类的所有上级分类
            updateParentLocationStatusNormal(zcLocation);
        }
        return result;
    }

    /**
     * 批量删除资产位置（树）
     * 
     * @param lcIds 需要删除的资产位置（树）主键
     * @return 结果
     */
    @Override
    public int deleteZcLocationByLcIds(Long[] lcIds)
    {
        return zcLocationMapper.deleteZcLocationByLcIds(lcIds);
    }

    /**
     * 删除位置信息 del_flag 标志位置 '2'
     *
     * @param lcId 位置ID
     * @return 结果
     */
    @Override
    public int deleteZcLocationByLcId(Long lcId)
    {
        return zcLocationMapper.deleteZcLocationByLcId(lcId);
    }

    /**
     * 根据ID查询所有子分类（正常状态）
     * @param lcId 位置ID
     * @return 子分类个数
     */
    @Override
    public int selectNormalChildrenLocationByLcId(Long lcId){
        return zcLocationMapper.selectNormalChildrenLocationByLcId(lcId);
    }

    /**
     * 修改子元素关系
     *
     * @param deptId 被修改的位置ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateLocationChildren(Long lcId, String newAncestors, String oldAncestors) {
        List<ZcLocation> children = zcLocationMapper.selectChildrenLocationByLcId(lcId);
        for (ZcLocation child : children) {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            zcLocationMapper.updateLocationChildren(children);
        }
    }

    public void updateParentLocationStatusNormal(ZcLocation zcLocation) {
        String ancestors = zcLocation.getAncestors();
        Long[] locations = Convert.toLongArray(ancestors);
        zcLocationMapper.updateLocationStatusNormal(locations);
    }

    /**
     * 是否存在分类子节点
     *
     * @param lcId 位置ID
     * @return 结果
     */
    public boolean hasChildByLcId(Long lcId){
        int result = zcLocationMapper.hasChildByLcId(lcId);
        return result > 0;
    }

}
